package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.entity.business.TpProductActiveSq;
import com.ys.entity.finance.TcwElectronicInvoice;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.entity.sys.TbCompanyInfo;
import com.ys.service.sys.CommonApiService;
import com.ys.util.AESUtil;
import com.ys.vo.W;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/web/finance/tcwElectronicInvoice")
public class WebTcwElectronicInvoiceController {


	@Autowired
	private CommonApiService<TcwElectronicInvoice> commonApiService;

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
						 @RequestParam Map<String, Object> params){

		if (params.get("bid") == null){
			return W.error(model, "单号不能为空");
		}
		TcwElectronicInvoice entity = new TcwElectronicInvoice().setBid((params.get("bid").toString()))
				.queryFirst();
		if (entity == null){
			return W.error(model, "该单据不存在!");
		}
		//查询购买方税号
		ThrOperateCompanyApply thrOperateCompanyApply = new ThrOperateCompanyApply().setVgsbm(entity.getVgsbmBuy()).queryFirst();
		//查询销售公司税号
		TbCompanyInfo tbCompanyInfo = new TbCompanyInfo().setVid(entity.getVgsbmSales()).queryFirst();
		Map<String, Object> extraParam = new HashMap<String, Object>();
		extraParam.put("vGsShBuy", thrOperateCompanyApply.getVgsSh());
		extraParam.put("vGsShSales", tbCompanyInfo.getTax());
		try {
			return commonApiService.toEdit(model, entity.getId(), new TcwElectronicInvoice(), MenuId.TCW_ElectronicInvoice, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return com.bo.vo.W.error(model, e.getMessage());
		}
	}
}
